﻿using System;
using System.Collections.Generic;
using UnityEngine;

 public partial class ASortingOrderManager:MonoBehaviour
{
    [SerializeField]
    private int minLayerNum;

    [SerializeField]
    private int maxLayerNum;

    private List<UGUISortingOrder> children = new List<UGUISortingOrder>();

    public void RegistChildNode(UGUISortingOrder child)
    {
        if (children.Count == 0)
        {
            InsertUGUIElement(child, 0, minLayerNum, maxLayerNum);
        }

        int preIndex = minLayerNum;
        int folIndex = maxLayerNum;

        for (int i = 0; i < children.Count; ++i)
        {
            children[i].UpdataSortingId();
            if (UGUISortingOrder.CompareSortingId(child.SortingId, children[i].SortingId) > 0)
            {
                folIndex = children[i].LayerIndex;

                InsertUGUIElement(child, i, preIndex, folIndex);

                return;
            }
            preIndex = children[i].LayerIndex;
        }

        InsertUGUIElement(child, children.Count, preIndex, folIndex);
    }

    private bool InsertUGUIElement(UGUISortingOrder child, int insertIndex, int preIndex, int folIndex)
    {
        int layerIndex = (preIndex + folIndex) / 2;
        if (layerIndex == preIndex)
        {
#if UNITY_EDITOR
            //Debug.LogWarning("DmUGUISortingOrderManager:Has no space to allocate layer index!!!");
#endif
            if (maxLayerNum - minLayerNum < children.Count)
            {
#if UNITY_EDITOR
                Debug.LogError("DmcUGUISortingOrderManager:Has no space to reallocate layer index!!!");
#endif
            }
            else
            {
                //ToDo: ReAllocate all space;
                children.Insert(insertIndex, child);
                ReallocateAllChildrenLayer();

                return true;
            }

            return false;
        }

        child.SetCanvasLayer(layerIndex);
        children.Insert(insertIndex, child);

        return true;
    }

    public bool IsChildRelativePathChanged(UGUISortingOrder child)
    {
        if (children.Contains(child))
        {
            int index = children.IndexOf(child);
            if (index != 0)
            {
                if (UGUISortingOrder.CompareSortingId(child.SortingId, children[index - 1].SortingId) > 0)
                {
                    return true;
                }
            }
            if (index < (children.Count - 1))
            {
                if (UGUISortingOrder.CompareSortingId(child.SortingId, children[index + 1].SortingId) < 0)
                {
                    return true;
                }
            }

            if (index > 0)
            {
                if (UGUISortingOrder.CompareSortingId(child.SortingId, children[index - 1].SortingId) > 0)
                {
                    return true;
                }
            }
        }
        return false;
    }

    private void ReallocateAllChildrenLayer()
    {
        int totalSpace = maxLayerNum - minLayerNum + 1;
        int childrenNum = children.Count;
        if (totalSpace == childrenNum)
        {
            for (int i = 0; i < children.Count; ++i)
            {
                children[i].SetCanvasLayer(minLayerNum + i);
            }
        }
        else
        {
            int spaceFragment = totalSpace / childrenNum;
            for (int i = 0; i < children.Count; ++i)
            {
                // maybe you cuo
                children[i].SetCanvasLayer(minLayerNum + (i + 1) * spaceFragment);
            }
        }

    }

    public void UnRegistChildNode(UGUISortingOrder child)
    {
        if (children.Contains(child))
        {
            children.Remove(child);
        }
    }

#region 单例,MonoBehaviour
    private void Awake()
    {
        instance = this;

    }

    void OnEnable()
    {

    }
    void OnDestroy()
    {

    }

    public static ASortingOrderManager Instance
    {
        get
        {
            return instance;
        }
    }

    private static ASortingOrderManager instance;
#endregion



}
